package cn.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jt on 2018/3/22.
 */
public class TeacherSubject implements Subject {

    /**
     * 用来存放和记录观察者
     */
    private List<Observer> observers = new ArrayList<>() ;

    /**
     * 记录状态的字符串
     */
    private String info ;

    @Override
    public void addObserver(Observer obj) {
        observers.add(obj) ;
    }

    @Override
    public void deleteObserver(Observer obj) {
        int i = observers.indexOf(obj) ;
        if(i >= 0){
            observers.remove(obj) ;
        }
    }

    @Override
    public void notifyObserve() {
        for(int i = 0 ; i < observers.size() ; i ++){
            Observer o = observers.get(i) ;
            o.update(info) ;
        }
    }

    public void setHomework (String info){
        this.info = info ;
        System.out.println("今天的作业是：" + info);
        this.notifyObserve() ;
    }
}
